﻿using System.Net.Http.Headers;
using System.Text;

namespace W.Common;

public class HttpClientWrapper
{
    private readonly HttpClient _httpClient;

    public HttpClientWrapper(string token)
    {
        _httpClient = new HttpClient();
        _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    }

    public async Task<string> GetAsync(string url)
    {
        var response = await _httpClient.GetAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    public async Task<string> PostAsync(string url, string content)
    {
        var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
        var response = await _httpClient.PostAsync(url, httpContent);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    public async Task<string> PutAsync(string url, string content)
    {
        var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
        var response = await _httpClient.PutAsync(url, httpContent);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    public async Task<string> DeleteAsync(string url)
    {
        var response = await _httpClient.DeleteAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}
